package controllers.factories;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lib_model.Account.Account;
import lib_model.Account.Account_factory;
import lib_model.Factories.Stock_factory_history;
import lib_model.Factories.Stocks_factory;
import lib_model.Orther.ReturnObj;
import lib_model.Product.Products;

/**
 * Servlet implementation class AddStock
 */
@WebServlet("/factories/AddStock")
public class AddStock extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddStock() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			String name = request.getParameter("txtName");
			String quan = request.getParameter("txtQuan");
			RequestDispatcher dis = request
					.getRequestDispatcher("../factories/stock.jsp");
			if (name != null && quan != null) {
				Account acc = (Account) request.getSession().getAttribute(
						"User");
				String usn = acc.username;
				int fac_id = 0;
				fac_id = ((Account_factory) (new Account_factory(usn)
						.GetInformation().ObjResult)).factory_id_ref;
				if (fac_id == 0) {
					request.getSession().removeAttribute("User");
					dis = request.getRequestDispatcher("../pages/home.jsp");
				} else {
					System.out.println("g_acc,fac_id = " + acc.group_id_ref
							+ fac_id);
					Products p = (Products) (new Products().GetByName(name)).ObjResult;
					if (p != null) {
						Stocks_factory f = new Stocks_factory();
						f.factory_id_ref = fac_id;
						f.products_id_ref = p.id;
						ReturnObj obj = new ReturnObj();
						Stocks_factory f2 = (Stocks_factory) (f.GetInformation()).ObjResult;
						Stock_factory_history h = new Stock_factory_history();
						if (f2 != null) {
							System.out.println("quan1 " + f.quantity);
							int outQuan = f2.quantity;
							f2.quantity += Integer.parseInt(quan);
							h.factory_id_ref = fac_id;
							h.products_id_ref = f2.products_id_ref;
							
							
							h.username_ref = acc.username;
							h.description = "Increased " + Integer.parseInt(quan) + " " +
									"<br />Total : " + f2.quantity + " ( "+ outQuan +" => " + f2.quantity + " )";
							h.Add();
							obj = f2.Update();
						} else {
							f.quantity = Integer.parseInt(quan);
							// ////
							h.factory_id_ref = fac_id;
							h.products_id_ref = p.id;
							h.username_ref = acc.username;
							h.description = "Add new Stock : " + Integer.parseInt(quan) + "" +
							"<br />Total : " + quan;
							h.Add();
							obj = f.Add();
						}
						// //
						if (obj.Result == true)
							request.setAttribute("Mes", "Suscessfull !");
						else {
							request.setAttribute("Mes",
									"Error ! Can't create Stock for factory (MSSQL)");
						}
					} else {
						request.setAttribute("Mes",
								"Error ! Product not exist ");
					}
				}

			} else {
				request.setAttribute("Mes",
						"Error ! Can't create Stock for factory (Format)");
			}
			dis.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			RequestDispatcher dis = request
					.getRequestDispatcher("../factories/stock.jsp");
			request.setAttribute("Mes",
					"Error ! Can't create Stock for factory");
			dis.forward(request, response);
		}

	}
}
